package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.dao.ProductMapper;
import com.erp.programmer.dao.StockOrderMapper;
import com.erp.programmer.domain.Product;
import com.erp.programmer.domain.StockOrder;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.ProductDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.dto.StockOrderDto;
import com.erp.programmer.service.StockOrderService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czh
 * @since 2021-05-24
 */
@Service
public class StockOrderServiceImpl extends ServiceImpl<StockOrderMapper, StockOrder> implements StockOrderService {

    @Autowired
    private ProductMapper productMapper;


    public ResponseDto<PageDto<StockOrderDto>> getData(PageDto<StockOrderDto> pageDto){
        QueryWrapper<StockOrder> queryWrapper = new QueryWrapper<>();
        if("warehousePosition".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据商品品牌搜索
            queryWrapper.eq("warehouse_position",pageDto.getSearchContent());
        }
        if(pageDto.getPage() == null){
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<StockOrder> list = list(queryWrapper);
        List<StockOrderDto> stockOrderDtos = CopyUtil.copyList(list, StockOrderDto.class);

        stockOrderDtos.forEach(e->{
            Product product = productMapper.selectById(e.getProductId());
            ProductDto productDto = CopyUtil.copy(product, ProductDto.class);
            e.setProductDto(productDto);
        });

        PageInfo<StockOrder> pageInfo = new PageInfo<>(list);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(stockOrderDtos);
        return ResponseDto.success(pageDto);
    }

    public ResponseDto<PageDto<StockOrderDto>> delete(StockOrderDto stockOrderDto){

        StockOrder stockOrder = CopyUtil.copy(stockOrderDto, StockOrder.class);
        removeById(stockOrder);
        PageDto<StockOrderDto> pageDtoList = getData(new PageDto<StockOrderDto>()).getData();
        ResponseDto<PageDto<StockOrderDto>> responseDto = getData(pageDtoList);
        responseDto.setMsg("删除成功！");

        return responseDto;
    }

    public ResponseDto<PageDto<StockOrderDto>> edit(StockOrderDto stockOrderDto) {
        StockOrder stockOrder = CopyUtil.copy(stockOrderDto, StockOrder.class);
        updateById(stockOrder);

        PageDto<StockOrderDto> pageDtoList = getData(new PageDto<StockOrderDto>()).getData();
        ResponseDto<PageDto<StockOrderDto>> responseDto = getData(pageDtoList);
        responseDto.setMsg("修改成功！");

        return responseDto;
    }


    public ResponseDto<PageDto<StockOrderDto>> add(StockOrderDto stockOrderDto){

        StockOrder stockOrder = CopyUtil.copy(stockOrderDto, StockOrder.class);
        save(stockOrder);

        PageDto<StockOrderDto> pageDtoList = getData(new PageDto<StockOrderDto>()).getData();
        ResponseDto<PageDto<StockOrderDto>> responseDto = getData(pageDtoList);
        responseDto.setMsg("添加成功！");

        return responseDto;
    }

}
